<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="标题" prop="titleName">
                <el-input v-model="queryParams.titleName" placeholder="请输入标题" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="网站类型" prop="publisherType">
                <el-select v-model="queryParams.publisherType" placeholder="请选择网站类型">
                    <el-option v-for="dict in dict.type.website_type" :key="dict.value" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="发布时间" prop="publisherTime">
                <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange"
            :row-style="{ height: '30px' }" border :cell-style="{ padding: '0px' }" highlight-current-row>
            <el-table-column label="序号" type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
            <el-table-column label="标题" align="center" prop="titleName" :show-overflow-tooltip="true" />
            <el-table-column label="网站链接" align="center" prop="websiteName" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <template>
                        <el-button @click="handleClick(scope.row.websiteName)" type="text" style="font-size: 12px;">
                            {{ scope.row.websiteName }}
                        </el-button>
                    </template>
                </template>
            </el-table-column>
            <el-table-column label="发布时间" align="center" prop="publisherTime" width="100" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.publisherTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="网站类型" align="center" prop="publisherType" width="100" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.website_type" :value="scope.row.publisherType" />
                </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="creationName" width="100" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="60">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row, scope.$index + 1)"  title="编辑"></el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                        @click="handleDelete(scope.row)" title="删除"></el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改【请填写功能名称】对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <div v-if="opt != 'add'">
                <el-descriptions title="">
                    <el-descriptions-item label="序号">
                        <el-tag size="medium">{{ index }}</el-tag>
                    </el-descriptions-item>
                </el-descriptions>
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="标题" prop="titleName">
                    <el-input v-model="form.titleName" placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="网站类型" prop="publisherType">
                    <el-select v-model="form.publisherType" placeholder="请选择网站类型" style="width: 380px;">
                        <el-option v-for="dict in dict.type.website_type" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="网站链接" prop="websiteName">
                    <el-input v-model="form.websiteName" placeholder="请输入网站链接" />
                </el-form-item>
                <el-form-item label="显示排序" prop="orderNum">
                    <el-input-number style="width: 280px" v-model="form.orderNum" controls-position="right" :min="0" />
                </el-form-item>
                <el-form-item label="发布时间" prop="publisherTime">
                    <el-date-picker clearable v-model="form.publisherTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择发布时间" style="width: 380px;">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="配图">
                    <file-view :key="refreshKey" :type="fileType" :businessId="businessId"
                        :businessCode="businessCode"></file-view>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement } from "@/api/website/website";
import FileView from "@/components/FileView/index"
export default {
    name: "websiteManagement",
    dicts: ['website_type'],
    components: { FileView },
    data() {
        return {
            isShowBtn: true,
            newWebsiteAs: null,
            opt: "edit",
            businessCode: "website",
            businessId: null,
            fileType: 0,
            refreshKey: 0,
            index: null,
            // 日期范围
            dateRange: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 【请填写功能名称】表格数据
            managementList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 15,
                titleName: null,
                websiteName: null,
                publisherId: null,
                publisherName: null,
                publisherTime: null,
                publisherType: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                titleName: [
                    { required: true, message: "标题不能为空", trigger: "blur" },
                    { max: 50, message: '标题长度不能超过50', trigger: 'blur' }
                ],
                publisherType: [
                    { required: true, message: "网站类型不能为空", trigger: "change" },
                ],
                websiteName: [
                    { required: true, message: "网站名称不能为空", trigger: "blur" },
                    { max: 400, message: '网站名称长度不能超过400', trigger: 'blur' }
                ],
                publisherTime: [
                    { required: true, message: "发布时间不能为空", trigger: "blur" },
                ],
            }
        };
    },
    created() {
        this.newWebsiteAs = this.$route.query.websiteAs;
        if (this.newWebsiteAs == "1") {
            this.isShowBtn = false;
        } else {
            this.isShowBtn = true;
        }
        this.getList();
    },
    methods: {
        /** 查询【请填写功能名称】列表 */
        getList() {
            this.loading = true;
            listManagement(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                this.managementList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                titleName: null,
                websiteName: null,
                publisherId: null,
                orderNum: null,
                publisherName: null,
                publisherTime: null,
                publisherType: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.opt = "add";
            this.fileType = 0;
            this.form.publisherTime = this.showDate();
            this.businessId = this.guid();
            this.open = true;
            this.title = "添加网站管理信息";
            this.refreshKey++;
        },
        /** 修改按钮操作 */
        handleUpdate(row, index) {
            this.reset();
            this.opt = "edit";
            this.fileType = 1;
            this.businessId = row.id;
            const id = row.id;
            this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
            getManagement(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改网站管理信息";
            });
            this.refreshKey++;
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateManagement(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.refreshKey++;
                            this.getList();
                        });
                    } else {
                        this.form.id = this.businessId;
                        addManagement(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.refreshKey++;
                            this.getList();
                        });
                    }
                }
            });
        },
        indexMethod(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        showDate() {
            var mydate = new Date();
            var str = "" + mydate.getFullYear() + "-";
            str += (mydate.getMonth() + 1) <= 9 ? "0" + (mydate.getMonth() + 1) + "-" : mydate.getMonth() + 1 + "-";
            str += mydate.getDate() <= 9 ? "0" + mydate.getDate() : mydate.getDate();
            return str;
        },
        guid() {
            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }
            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除标题为"' + row.titleName + '"的数据项？').then(function () {
                return delManagement(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        handleClick(url) {
            if (!url.startsWith('http://') && !url.startsWith('https://')) {
                // 如果没有协议，添加默认的http://协议
                url = 'http://' + url;
            } window.open(url, '_blank');
        },
    }
};
</script>
